package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Bill;
import com.example.entity.User;
import com.example.service.BillService;
import com.example.service.BillVoService;
import com.example.vo.BillVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private BillVoService billVoService;


    @GetMapping("/all/proname")
    public R<List<String>> getDistinctPronames(){
        LambdaQueryWrapper<BillVo> queryWrapper = new LambdaQueryWrapper<>();

        //选择要查询的字段
        queryWrapper.select(BillVo::getProname);

        //添加去重条件
        queryWrapper.groupBy(BillVo::getProname);

        //执行查询
        List<Object> pronamesObjs = billVoService.listObjs(queryWrapper);
        List<String> pronames = new ArrayList<>();
        for (Object pronameObj : pronamesObjs) {
            pronames.add(String.valueOf(pronameObj));
        }

        return R.success(pronames);
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name, String provider, String isPay){
        log.info("page = {},pageSize = {},name = {},provider = {},isPay = {}" ,page,pageSize,name,provider,isPay);

        //构造分页构造器
//        Page<BillVo> pageInfo = new Page<>(page,pageSize);
        Page pageInfo = new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<BillVo> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),BillVo::getProductname,name);
        queryWrapper.eq(StringUtils.isNotEmpty(provider),BillVo::getProname,provider);
        queryWrapper.eq(StringUtils.isNotEmpty(isPay),BillVo::getIspayment,isPay);
        //添加排序条件
        queryWrapper.orderByDesc(BillVo::getCreationdate);

        //执行查询
        billVoService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }


    /**
     * 根据id查询订单信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<BillVo> getById(@PathVariable Long id){
        log.info("根据id查询订单信息...");
        BillVo billVo = billVoService.getById(id);
        if(billVo != null){
            return R.success(billVo);
        }
        return R.error("没有查询到对应订单信息");
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除订单，id为：{}",id);
        billService.removeById(id);
        return R.success("订单信息删除成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Bill bill){

        log.info("新增订单，订单信息：{}",bill.toString());


        //获得当前登录用户的id
        Long userId = (Long) request.getSession().getAttribute("user");

        bill.setCreatedby(userId);
        bill.setCreationdate(LocalDateTime.now());

        billService.save(bill);

        return R.success("新增订单成功");
    }


    /**
     * 根据id修改订单信息
     * @param bill
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Bill bill){

        log.info(bill.toString());

        long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        Long userId = (Long) request.getSession().getAttribute("user");
        bill.setModifyby(userId);
        bill.setModifydate(LocalDateTime.now());
        billService.updateById(bill);

        return R.success("订单信息修改成功");
    }



}